﻿/*===============================================================
 TASK 9:                                                {Methods}
 Write a method that adds two positive integer numbers represented
 as arrays of digits (each array element arr[i] contains a digit;
 the last digit is kept in arr[0]). Each of the numbers that
 will be added could have up to 10 000 digits.
 ===============================================================*/

using System;

class PortionOfArrayMaxElement
{
    static void Main()
    {
        int[] arr = { 3, 70, 60, 2, 5 };

        int max = GetMaximalElemnt(arr, 1, 2);
        int maxPosition = GetMaximalElemnt(arr, 1, 2, true);
        Console.WriteLine("max element is arr[{0}]={1}", maxPosition, max);

        //descending sort
        mySort(arr);
        PrintArray(arr);

        Console.WriteLine();

        //ascending sort
        mySort(arr, false);
        PrintArray(arr);
    }

    static void PrintArray(int[] arr)
    {
        foreach (var element in arr)
        {
            Console.Write(element + " ");
        }
    }
    static int GetMaximalElemnt(int[] arr, int startIndex, int endIndex, bool position = false)
    {
        int temp = 0;
        if (startIndex > endIndex)
        {
            temp = endIndex;
            endIndex = startIndex;
            startIndex = temp;
        }
        int maxElement = int.MinValue;
        int positionMax = 0;
       
        for (int i = startIndex; i <= endIndex; i++)
        {
            if (arr[i] > maxElement)
            {
                maxElement = arr[i];
                positionMax = i;
            }
        }
        if (position)
        {
            return positionMax;
        }
        else
        {
            return maxElement;
        }
    }


    static int[] mySort(int[] arr, bool descending = true)
    {
        int maxPosition = int.MinValue;
        int temp = 0;
        if (descending == false)
        {
            for (int i = arr.Length - 1; i >= 0; i--)
            {
                maxPosition = GetMaximalElemnt(arr, i, 0, true);
                temp = arr[i];
                arr[i] = arr[maxPosition];
                arr[maxPosition] = temp;
            }
        }
        else
        {
            for (int i = 0; i < arr.Length - 1; i++)
            {
                maxPosition = GetMaximalElemnt(arr, i, arr.Length - 1, true);
                temp = arr[i];
                arr[i] = arr[maxPosition];
                arr[maxPosition] = temp;
            }
        }
        return arr;
    }
}